package com.musifind.wsclient.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import com.musifind.wsclient.helpers.UserHelper;
import com.musifind.wsclient.objects.Matching;
import com.musifind.wsclient.objects.User;
import com.musifind.wsclient.url.MatchingRestURIConstants;

public class MatchingService extends BaseService {

	public static Matching createMatching(int fromUserId, int toUserId) {
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
		restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

		Matching matching = new Matching();
		User from = UserService.getUser(fromUserId);
		User to = UserService.getUser(toUserId);
		matching.setFrom(from);
		matching.setTo(to);
		matching.setFlag(false);
		matching.setTarih(new Date());
		Matching response = restTemplate.postForObject(SERVER_URI + MatchingRestURIConstants.CREATE_MATCH, matching,
				Matching.class);

		return response;
	}

	public static Matching getMatchingById(int id) {
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
		restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
		Matching response = restTemplate.getForObject(SERVER_URI + MatchingRestURIConstants.GET_MATCH_BY_ID + id,
				Matching.class);
		return response;
	}

	public static List<Matching> getMatchingsByUserId(int userId) {
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
		restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

		List<LinkedHashMap> matchingList = restTemplate.getForObject(SERVER_URI
				+ MatchingRestURIConstants.GET_MATCH_BY_USER + userId, List.class);

		List<Matching> matchings = new ArrayList<Matching>();
		for (LinkedHashMap map : matchingList) {
			Matching matching = new Matching();
			matching.setId(new Integer("" + map.get("id")));
			matching.setFrom(UserHelper.getUserFromLinkedHashMap((LinkedHashMap) map.get("from")));
			matching.setTo(UserHelper.getUserFromLinkedHashMap((LinkedHashMap) map.get("to")));
			matching.setTarih(new Date((Long) map.get("tarih")));
			matchings.add(matching);
		}

		return matchings;
	}

	public static Matching deleteMatching(int id) {
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
		restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
		Matching matching = restTemplate.getForObject(SERVER_URI + MatchingRestURIConstants.DELETE_MATCH + id,
				Matching.class);
		return matching;
	}

	// public static Matching updateMatching(int id) {
	// RestTemplate restTemplate = new RestTemplate();
	// restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
	// restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
	// Matching matching = restTemplate.getForObject(SERVER_URI + MatchingRestURIConstants.UPDATE_MATCH + id,
	// Matching.class);
	// return matching;
	// }

	public static Matching updateMatching(int id) {
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
		restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
		Matching matching = restTemplate.getForObject(SERVER_URI + "/matching/update/" + id, Matching.class);
		return matching;
	}

}
